package com.example.controller;




import com.example.domain.message.MessageConstant;
import com.example.domain.pojo.CheckGroup;
import com.example.domain.pojo.CheckItem;
import com.example.domain.query.QueryPageBean;
import com.example.domain.result.PageResult;
import com.example.domain.result.Result;
import com.example.service.CheckGroupService;
import com.example.service.CheckItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController //返回Jason数据的控制层
@RequestMapping("/checkGroup")
public class CheckGroupController {


   @RequestMapping("/login")
    public String getString(){
       return "string...";
   }

    //注入checkItemService
    @Autowired
    private CheckItemService checkItemService;

    @Autowired
    private CheckGroupService checkGroupService;

    //查询所有的检查项信息,动态显示在前段页面
    @RequestMapping("/findAllCheckItem")
    public Result findAllCheckItem(){

        try {
            List<CheckItem> allCheckItem = checkItemService.findAllCheckItem();
            return  new Result(true, MessageConstant.QUERY_CHECKITEM_SUCCESS,allCheckItem);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_CHECKITEM_FAIL);
        }
    }
    //添加检查组
    @RequestMapping("/addGruop")
    public Result addGroup(@RequestBody CheckGroup checkGroup, Integer[] checkitemIds){
        try {
            checkGroupService.addGroup(checkGroup,checkitemIds);
            return new Result(true,MessageConstant.ADD_CHECKGROUP_SUCCESS);
        } catch (Exception e) {
            return new Result(false,MessageConstant.ADD_CHECKGROUP_FAIL);
        }
    }
    //分页查询检查分组数据
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        System.out.println(queryPageBean);
        //1;直接传递page参数信息
        PageResult page = checkGroupService.findPage(queryPageBean);
        //2:直接将分页数据返回,没有用result封装
        return page;
    }
    //根据检查组id查询检查组信息
    @RequestMapping("/findCheckGroupById")
    public CheckGroup findCheckGroupById(Integer id){
        //有可能异常,需要返回message提示信息,可是只要将检查组封装为result,就是双层for循环
        //前端的获取??
        CheckGroup checkGroupById = checkGroupService.findCheckGroupById(id);
        return checkGroupById;
    }

    //根据检查组id查询检查项id回显
    @RequestMapping("/findItemIdByGroupId")
    public Integer[] findItemIdByGroupId(Integer id){
        Integer[] ids = checkGroupService.findItemIdByGroupId(id);
        return ids;
    }

    //checkGroup/editCheckGroup.do?checkitemIds="
    //提交编辑数据
    @RequestMapping("/editCheckGroup")
    public Result editCheckGroup(@RequestBody CheckGroup checkGroup,Integer[] checkitemIds){

        try {
            //1:调用方法编辑修改检查组基本信息
            checkGroupService.editGroup(checkGroup);
            //2:修改中间表
            checkGroupService.editGroupAndItem(checkGroup.getId(),checkitemIds);

            return new Result(true,MessageConstant.EDIT_CHECKGROUP_SUCCESS);
        } catch (Exception e) {
//            e.printStackTrace();
            return new Result(false,MessageConstant.EDIT_CHECKGROUP_FAIL);
        }
    }

    //删除检查组
    @RequestMapping("/deleteGroup")
    public Result deleteGroup(Integer id){
        try {
            checkGroupService.deleteGroup(id);
            return new Result(true, MessageConstant.DELETE_CHECKGROUP_SUCCESS);
        } catch (Exception e) {
//            e.printStackTrace();

            return new Result(false,MessageConstant.DELETE_CHECKGROUP_FAIL);
        }

    }
}
